import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    //层序遍历:
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> list = new LinkedList<>();
        List<List<Integer>> ret = new LinkedList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) {
            return list;
        }
        //.......
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> tmp = new LinkedList<>();
            for (int i = 0; i < size; i++) {
                TreeNode top = queue.poll();
                tmp.add(top.val);
                //
                if (top.left != null) {
                    queue.offer(top.left);
                }
                if (top.right != null) {
                    queue.offer(top.right);
                }

            }
            list.add(tmp);
        }
        for (int i = list.size() - 1; i >= 0; i--) {
            ret.add(list.get(i));
        }
        return ret;
    }

    //................................................................................
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ret = new LinkedList<>();// 要返回的
        Queue<TreeNode> queue = new LinkedList<>();// 队列
        if (root == null) {
            return ret;
        }
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> list = new LinkedList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                list.add(cur.val);
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            ret.add(list);
        }
        return ret;
    }

    public String tree2str(TreeNode root) {
        StringBuilder str = new StringBuilder();
        test(root, str);
        return str.toString();
    }

    public void test(TreeNode root, StringBuilder str) {
        if (root == null) {
            return;
        }
        //处理根节点
        str.append(root.val);

        //处理左树
        if (root.left != null) {
            str.append("(");
            test(root.left, str);//递归左树
            str.append(")");
        } else {
            if (root.right == null) {
                return;
            } else {
                str.append("()");
            }
        }

        //处理右树
        if (root.right != null) {
            str.append("(");
            test(root.right, str);//递归左树
            str.append(")");
        } else {
            return;
        }
    }

    public static void main(String[] args) {

    }
}